package com.test;

import java.util.concurrent.TimeUnit;

public class VolatileDemo
{

    public static void visibilityByVolatile()
    {
        int j = 0;
        MyData myData = new MyData();

        // 第一个线程
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in");
            try
            {
                // 线程暂停3s
                TimeUnit.SECONDS.sleep(1);
                myData.addToSixty();

                System.out.println(Thread.currentThread().getName() + "\t update value:" + myData.num);
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }, "thread1").start();
        // 第二个线程是main线程
        while (myData.num == 0)
        {
            System.out.print("");
            // 如果myData的num一直为零，main线程一直在这里循环
            synchronized (Solution.class)
            {

            }

        }
        System.out.println(Thread.currentThread().getName() + "\t mission is over, num value is " + myData.num);
    }

    public static void main(String[] args)
    {
        visibilityByVolatile();// 验证volatile的可见性

    }
}

class MyData
{
    int num = 0;
//volatile int num = 0;

    public void addToSixty()
    {
        this.num = 60;
    }

}